package com.glela.goods.service.impl;

import com.glela.common.constant.CommonConstants;
import com.glela.goods.mapper.NewsMapper;
import com.glela.goods.model.NewsType;
import com.glela.goods.model.vo.NewsVo;
import com.glela.goods.model.vo.TopNewsVo;
import com.glela.goods.service.NewsService;
import com.glela.platform.model.BaseRestrictions;
import com.glela.platform.service.BaseRestrictionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class NewsServiceImpl implements NewsService{

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private BaseRestrictionsService baseRestrictionsService;

    @Override
    public List<TopNewsVo> getTopNews(HttpServletRequest request) {
        List<TopNewsVo> list = newsMapper.getTopNews(CommonConstants.NEW_GG_ID, CommonConstants.NEW_GG_SHOW_NUM);
        BaseRestrictions imageBR = baseRestrictionsService.getBaseRestrictionsForCache("image_url");
        for (TopNewsVo top: list) {
            if(request.getHeader("phoneType").toLowerCase().equals(CommonConstants.PHONE_CLIENT_IOS)){
                top.setInfoUrl(imageBR.getName()+top.getInfoUrl()+"?ios=1");
            } else if(request.getHeader("phoneType").toLowerCase().equals(CommonConstants.PHONE_CLIENT_ANDROID)){
                top.setInfoUrl(imageBR.getName()+top.getInfoUrl()+"?android=1");
            }
        }
        return list;
    }

    @Override
    public List<NewsVo> getByTypeId(int typeId,int startIndex,int num) {
        List<NewsVo> list = newsMapper.getByTypeId(typeId,startIndex,num);
        return list;
    }

    @Override
    public int getCountByTypeId(int typeId) {
        return newsMapper.getCountByTypeId(typeId);
    }

    @Override
    public List<NewsType> getNewComerCrashType() {
        return newsMapper.getNewComerCrashType();
    }
}
